﻿

$.widget( "custom.catcomplete", $.ui.autocomplete, {
    _renderMenu: function( ul, items ) {
        var that = this,
            currentCategory = "";
        $.each( items, function( index, item ) {
            if ( item.category != currentCategory ) {
                ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
                currentCategory = item.category;
            }
            that._renderItemData( ul, item );
        });
    }
});

var data;
var organizedData = [];
$.ajax({
    type: "GET",
    async:"false",
    url: "/MyServices.svc/getAutocompleteData",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        data = msg.d;
        $(document).ready(function () {
            $.each(data, function (key, value) {
                $.each(value, function (k, v) {
                    if (k != "__type") {
                        var values = { label: v, category: k };
                        var inArray = $.inArray(values, organizedData);
                        if (inArray == -1) {
                            organizedData.push({ label: v, category: k });
                        }
                    }
                });

            });

            var search = $(".searchContainer").children("input");
            $(search).catcomplete({ delay: 0, source: organizedData});

        });
    }

});
